package views;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import presenters.AdminListPresenter;
import com.example.wheresmystuff.R;
import data.Account;
import data.DBInterfaceError;
import data.GetAccountList;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * activity for admin list
 * 
 * @author Kelsey
 */
public class AdminListActivity extends Activity implements IListView {

	/** presenter for admin list */
	AdminListPresenter myPresenter;

	/** listview of accounts */
	private ListView accountList;

	/** list of accounts */
	private List<Account> accounts;

	/** need reference to this so an anonymous inner class can use it */
	private AdminListActivity adminListActivity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_admin_list);

		adminListActivity = this;

		accountList = (ListView) findViewById(R.id.listView1);

		// setup account list click handler
		accountList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(adminListActivity,
						AdminAccountActivity.class);
				intent.putExtra("account", accounts.get(position)
						.getAccountName());
				startActivity(intent);
				finish();
			}
		});

		updateAccountList();

		myPresenter = new AdminListPresenter(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.admin_list, menu);
		return true;
	}

	public void onExitAdminClick(View source) {
		myPresenter.onExitAdminClick(source);
	}

	/**
	 * Updates the account list
	 */
	@SuppressLint("NewApi")
	public void updateAccountList() {
		accounts = null;
		try {
			GetAccountList getAccounts = new GetAccountList(this);
			accounts = getAccounts.getResult();
		} catch (DBInterfaceError e) {
			e.printStackTrace();
		}

		List<String> accountStrs = new ArrayList<String>();
		if (accounts != null) {
			for (int i = 0; i < accounts.size(); i++) {
				Account account = accounts.get(i);
				String accountText = "User: " + account.getAccountName()
						+ "\nLocked: " + account.getIsLocked() + "\nAdmin: "
						+ account.getIsAdmin();

				accountStrs.add(accountText);
			}
		}

		String[] values = Arrays.copyOf(accountStrs.toArray(),
				accountStrs.size(), String[].class); // this shit is crazy
														// because you can't
														// cast a Object[] array
														// to at String[] array

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, android.R.id.text1, values);
		accountList.setAdapter(adapter);
	}

	@Override
	public void advance(View source) {
		Intent intent;
		switch (source.getId()) {
		case R.id.button1: // exit button
			intent = new Intent(this, MainListActivity.class);
			startActivity(intent);
			finish();
		}
	}

	@Override
	public void update(View source) {

	}

}